package com.jlcloud.core.loadbalancer.rule;

import com.jlcloud.core.auto.annotation.AutoEnvPostProcessor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.StringUtils;

/**
 * 灰度版本 自动处理
 *
 * @author By: JlCloud <br>
 * @Package: com.jlcloud <br>
 * @CreateTime: 2024-12-03 21:45 <br>
 * @Copyright: 2021 www.jilianjituan.com Inc. All rights reserved. <br>
 * @Caution 注意：本内容仅限于冀联人力集团内部传阅，禁止外泄以及用于其他的商业目的 <br>
 */
@AutoEnvPostProcessor
public class GrayscaleEnvPostProcessor implements EnvironmentPostProcessor, Ordered {
	private static final String GREYSCALE_KEY = "jlcloud.loadbalancer.version";
	private static final String ELK_KEY = "jlcloud.log.elk.destination";

	private static final String METADATA_KEY = "spring.cloud.nacos.discovery.metadata.version";

	@Override
	public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
		String version = environment.getProperty(GREYSCALE_KEY);
		if (StringUtils.hasText(version)) {
			environment.getSystemProperties().put(METADATA_KEY, version);
		}

		String elk = environment.getProperty(ELK_KEY);
		if (StringUtils.hasText(elk)) {
			environment.getSystemProperties().put(ELK_KEY, elk);
		}
	}

	@Override
	public int getOrder() {
		return Ordered.LOWEST_PRECEDENCE;
	}

}
